//
//  MineSmallView.m
//  礼物go
//
//  Created by dllo on 16/5/30.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MineSmallView.h"
#import "SmallTableViewCell.h"

@implementation MineSmallView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getsmallDataArray:) name:@"giftTableViewData" object:nil];
        
    // 高度减去titlescrollview的高度
        self.smallTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame) - 44) style:UITableViewStylePlain];
        self.smallTableView.delegate = self;
        self.smallTableView.dataSource = self;
        [self addSubview:self.smallTableView];
        [self.smallTableView registerClass:[SmallTableViewCell class] forCellReuseIdentifier:@"SmallTableViewCell"];
        self.smallTableView.scrollsToTop = YES;
        // 注册滑动到顶部的通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:@"goTop" object:nil];
        
        // 注册离开顶部的通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:@"leaveTop" object:nil];
        
        
    }

    return self;
}

// 接收通知
-(void)notificationAction:(NSNotification *)notification{
// 如果是滑动到顶部通知
NSString *notificationName = notification.name;
    if ([notificationName isEqualToString:@"goTop"]) {
        NSDictionary *userInfo = notification.userInfo;
        NSString *canScroll = userInfo[@"canScroll"];
        if ([canScroll isEqualToString:@"1"]) {
            self.canScroll = YES;
            self.smallTableView.showsVerticalScrollIndicator = NO;
        }
    }

    if ([notificationName isEqualToString:@"leaveTop"]) {
         // 离开顶部,不允许小的动了
        self.smallTableView.contentOffset = CGPointZero;
        self.canScroll = NO;
        self.smallTableView.showsVerticalScrollIndicator = NO;
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
     // 如果不能动则保持(0,0)的偏移量
    if (!self.canScroll) {
     [scrollView setContentOffset:CGPointZero];
    }

    CGFloat offsetY = scrollView.contentOffset.y;
    //NSLog(@"small tableview offse=====%f",offsetY);
    if (offsetY <= 0) {
        
        // 发出离开置顶标题位置
        [[NSNotificationCenter defaultCenter] postNotificationName:@"leaveTop" object:nil userInfo:@{@"canScroll":@"1"}];
    }
}

-(void)getsmallDataArray:(NSNotification *)notification{

    self.dataArray = [NSMutableArray arrayWithArray:[notification object]];
   // NSLog(@"%ld", _dataArray.count);
    [self.smallTableView reloadData];
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SmallTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SmallTableViewCell"];
    cell.seleDetail = [self.dataArray objectAtIndex:indexPath.row];
    
    return cell;
   // return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 80;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SelectDetailModel *selectDetail = [self.dataArray objectAtIndex:indexPath.row];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"postPush" object:selectDetail.myId];
    


}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
